package com.by.service.impl;

import com.by.bean.Enquiry;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.utils.Assert;
import com.by.mapper.EnquiryDetailMapper;
import com.by.mapper.EnquiryMapper;
import com.by.service.CustomerService;
import com.by.service.EnquiryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class EnquiryServiceImpl extends BaseServiceImpl<Enquiry, EnquiryMapper> implements EnquiryService {
    @Autowired
    EnquiryDetailMapper enquiryDetailMapper;
    @Autowired
    EnquiryMapper enquiryMapper;
    @Autowired
    CustomerService customerService;

    @Transactional
    @Override
    public int save(Enquiry entity) {
        //保存询价单
        entity.setSaler(customerService.get(entity.getCreateUserId()).getName());
        entity.setMobile(customerService.get(entity.getCreateUserId()).getMobile());
        int rs = super.save(entity);
        Assert.isNotSuccess(rs, "保存询价单失败");
        //保存明细
        if (null != entity.getEnquiryDetails() && entity.getEnquiryDetails().size() > 0) {
            //批量保存
            rs = enquiryDetailMapper.batchSave(entity.getId(), entity.getEnquiryDetails());
            Assert.isNotSuccess(rs, "保存询价明细失败");
        }

        return 1; // 返回保存的记录数，或 0 表示失败

    }
}
